
/**
 * @description 全局事件通信,仅限与页面未重载情况下
 * @param {String} eventName 
 * @param {any} data 
 */
export function registeredEvents(eventName = '', data = null) {
    window.__evs__ ? null : window.__evs__ = {}
    window.__evs__[eventName] = new CustomEvent(eventName, { bubbles: true, cancelable: true, detail: data });
    window.dispatchEvent(window.__evs__[eventName]);
}

/**
 * @name setSessionStorage
 * @param {object} param
 */
export function setSessionStorage(param = {}) {
    for (let i in param) {
        if (typeof param[i] == "object") {
            try {
                param[i] = JSON.stringify(param[i]);
            } catch (error) { }
        }
        sessionStorage.setItem(i, param[i]);
    }
}
/**
 * @name getSessionStorage
 * @param {Array} key
 * @param {Boolean} isStrung
 * @returns
 */
export function getSessionStorage(key = [], isStrung = true) {
    let session = {};
    for (let i = 0; i < key.length; i++) {
        session[key[i]] = sessionStorage.getItem(key[i]);
        if (!isStrung) {
            try {
                session[key[i]] = JSON.parse(session[key[i]]);
            } catch (error) { }
        }
    }
    return session;
}

/**
 * @name removeSessionStorage
 * @param {String} key
 */
export function removeSessionStorage(key) {
    if (key) {
        switch (true) {
            case "forEach" in key:
                key.forEach((i) => {
                    sessionStorage.removeItem(i);
                });
                break;
            default:
                sessionStorage.removeItem(key);
                break;
        }
    } else {
        sessionStorage.clear();
    }
}

/**
 * 进件上传图片
 * @param {*} url 
 * @returns 
 */
export function customBaseUrl(url=''){
    return process.env.VUE_APP_UPLOAD_IMG_URL + url
}